package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.hjpbean.GeneralNewsBean;
import net.oschina.app.ui.ZiXunActivity;
import net.oschina.app.util.BaseRecyclerAdapter;
import net.oschina.app.util.RecyclerViewHolder;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Admin on 2017/7/15.
 */

public class ZongHeFangment extends BaseFragment {

    private List<GeneralNewsBean.NewsBean> newsLists;
    @BindView(R.id.rcv_news_zixun)
    RecyclerView rcvNewsZixun;
    @BindView(R.id.swipe_refresh_widget)
    SwipeRefreshLayout swipeRefreshWidget;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View zonghefangment = inflater.inflate(R.layout.fragment_default, container, false);
        unbinder = ButterKnife.bind(this, zonghefangment);
        return zonghefangment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getshuju();
//        String s=news.getUrl().toString();
        Log.d("baima", 123 + "");

    }

    public void getshuju() {
        retrofit2.Call<GeneralNewsBean> info = Myapp.shopService.getInfo(0, 1, 20);
        info.enqueue(new Callback<GeneralNewsBean>() {
            @Override
            public void onResponse(retrofit2.Call<GeneralNewsBean> call, Response<GeneralNewsBean> response) {

                newsLists = response.body().getNewslist();

//                Log.d("baima", newsLists.get(15).getUrl().toString());a

                initView();

            }

            @Override
            public void onFailure(retrofit2.Call<GeneralNewsBean> call, Throwable t) {

//                initView();

                t.printStackTrace();

            }
        });

    }

    private void initView() {

        rcvNewsZixun.setLayoutManager(new LinearLayoutManager(getContext()));


//        LinearLayoutManager manager = new LinearLayoutManager(getContext());
//        zongheZixunRcv.setLayoutManager(manager);

        RecyclerView.Adapter adapter = new BaseRecyclerAdapter<GeneralNewsBean.NewsBean>(getContext(), newsLists) {
            @Override
            public int getItemLayoutId(int viewType) {

                return R.layout.zonghe_rcv_item;

            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position, GeneralNewsBean.NewsBean item) {

                Log.d("baima", "到此一游");
                holder.getTextView(R.id.zonghe_zixunrcv_tvtitle).setText(newsLists.get(position).getTitle());
                holder.getTextView(R.id.zonghe_zixunrcv_tvneirong).setText("        " + newsLists.get(position).getBody());
                holder.getTextView(R.id.zonghe_zixunrcv_yonghuming).setText(newsLists.get(position).getAuthor());
                holder.getTextView(R.id.zonghe_zixunrcv_tvtime).setText(newsLists.get(position).getPubDate());
                holder.getTextView(R.id.zonghe_zixunrcv_tvpinglun).setText(newsLists.get(position).getCommentCount());

            }


        };

        rcvNewsZixun.setAdapter(adapter);

        ((BaseRecyclerAdapter) adapter).setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int pos) {

//                    Toast.makeText(getContext(),newsLists.get(1).getTitle(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), ZiXunActivity.class);
                intent.putExtra("title","资讯详情");
                intent.putExtra("id", newsLists.get(pos).getId());
                startActivity(intent);

            }
        });


    }


}
